<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HanLP 客户端</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            text-align: center;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        pre {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
            white-space: pre-wrap;
        }
        .loading {
            text-align: center;
            display: none;
        }
        .spinner {
            border: 4px solid rgba(0, 0, 0, 0.1);
            width: 36px;
            height: 36px;
            border-radius: 50%;
            border-left-color: #09f;
            animation: spin 1s linear infinite;
            display: inline-block;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <h1>HanLP 自然语言处理工具</h1>
    
    <div class="form-group">
        <label for="text">输入文本:</label>
        <textarea id="text" rows="5" placeholder="请输入中文文本...">今天天气真好，我想去公园散步。北京大学的小明正在研究人工智能。"></textarea>
    </div>
    
    <div class="form-group">
        <label for="api">选择功能:</label>
        <select id="api">
            <option value="tokenize">分词</option>
            <option value="pos">词性标注</option>
            <option value="ner">命名实体识别</option>
            <option value="dep">依存句法分析</option>
            <option value="mtl">多任务处理</option>
        </select>
    </div>
    
    <div class="form-group">
        <button onclick="processText()">处理</button>
    </div>
    
    <div class="loading" id="loading">
        <div class="spinner"></div>
        <p>处理中，请稍候...</p>
    </div>
    
    <div class="form-group">
        <label>结果:</label>
        <pre id="result">结果将显示在这里...</pre>
    </div>
    
    <script>
        const SERVER_URL = 'http://localhost:5000';
        
        async function processText() {
            const text = document.getElementById('text').value.trim();
            if (!text) {
                alert('请输入文本');
                return;
            }
            
            const api = document.getElementById('api').value;
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            
            loading.style.display = 'block';
            result.textContent = '处理中...';
            
            try {
                const response = await fetch(`${SERVER_URL}/api/${api}`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ text: text })
                });
                
                if (!response.ok) {
                    throw new Error(`服务器响应错误: ${response.status}`);
                }
                
                const data = await response.json();
                if (data.code === 0) {
                    result.textContent = JSON.stringify(data.result, null, 2);
                } else {
                    result.textContent = `错误: ${data.message}`;
                }
            } catch (error) {
                result.textContent = `请求错误: ${error.message}\n请确保HanLP服务器已启动`;
            } finally {
                loading.style.display = 'none';
            }
        }
    </script>
</body>
</html> 